var express = require("express");
var { createToken, checkToken } = require("../utils/token");
var axios = require("axios");
var router = express.Router();
var { LiuYan, Prouser, Volume, GongGao, Reactuser } = require("../utils/model");
router.get("/apidom", (req, res) => {
  res.json({
    code: 200,
    msg: "连接成功",
    result: [],
  });
});

router.get("/getcomments", (req, res) => {
  LiuYan.find({}, {})
    .sort({
      _id: 1,
    })
    .then((result) => {
      res.json({
        code: 200,
        msg: "获取留言成功",
        result,
      });
    });
});

router.post("/updatecomment", (req, res) => {
  var { _id, content } = req.body;
  LiuYan.update(
    {
      _id,
    },
    {
      $set: {
        content: content,
      },
    }
  ).then((result) => {
    res.json({
      code: 200,
      msg: "修改留言成功",
      result,
      type: 1,
    });
  });
});

router.get("/delcomment", (req, res) => {
  var _id = req.query._id;
  LiuYan.remove({
    _id,
  }).then((result) => {
    res.json({
      code: 200,
      msg: "删除留言成功",
      result,
      type: 1,
    });
  });
});

router.post("/addcomment", (req, res) => {
  const { title, content } = req.body;
  LiuYan.insertMany({
    title,
    content,
  }).then((data) => {
    LiuYan.find({}, {})
      .sort({
        _id: 1,
      })
      .then((result) => {
        res.json({
          code: 200,
          msg: "插入留言成功",
          result,
        });
      });
  });
});

//react
router.get("/getcrsftoken", (req, res) => {
  const token = createToken({ referer: req.headers.referer, data: new Date() });
  res.json({
    msg: "token获取成功",
    code: 200,
    type: 1,
    result: token,
  });
});

// 密码登录
router.post("/loginpass", (req, res) => {
  var body = req.body;
  Prouser.findOne({
    phone: body.phone,
    pass: body.pass,
  })
    .then((result) => {
      if (result) {
        const token = createToken({ phone: body.phone, date: new Date() });
        res.json({
          code: 200,
          result,
          msg: "登录成功",
          type: 1,
          token,
        });
      } else {
        res.json({
          code: 200,
          msg: "登录失败",
          type: 0,
        });
      }
    })
    .catch((err) => {
      res.json({
        code: 500,
        msg: "服务器异常",
        type: 0,
      });
    });
});

// 验证码登录
router.post("/logincaptcha", (req, res) => {
  var body = req.body;
  axios
    .get("https://shiwenmz.top:3333/captcha/verify", {
      params: body,
    })
    .then((obj) => {
      let { data } = obj;
      if ((data.code = 200)) {
        Prouser.findOne({
          phone: body.phone,
        }).then((result) => {
          if (result) {
            const token = createToken({ phone: body.phone, date: new Date() });
            res.json({
              code: 200,
              msg: "登录成功",
              type: 1,
              result,
              token,
            });
          } else {
            res.json({
              code: 200,
              msg: "登录失败,请联系超级管理员",
              type: 0,
            });
          }
        });
      }
    })
    .catch((err) => {
      res.json({
        code: 200,
        msg: "验证码错误",
        type: 0,
        err,
      });
    });
});

//获取用户信息
router.get("/getuserinfo", (req, res) => {
  checkToken(req, res, (data) => {
    Prouser.findOne({ phone: data.phone }).then((result) => {
      res.json({
        code: 200,
        msg: "获取数据成功",
        type: 1,
        result,
      });
    });
  });
});

//图片上传
var multer = require("multer");
var storage = multer.diskStorage({
  destination: function (req, file, cd) {
    cd(null, "./public/avatar"); //设置图片存放的路径
  },
  filename: function (req, file, cd) {
    cd(null, Date.now() + "wh2006" + file.originalname);
  },
});
var upload = multer({ storage: storage }).any(); // 可以上传任何格式的图片

router.post("/uploadavatar", upload, (req, res) => {
  if (req.files) {
    var avatar = req.files[0].path;
    Prouser.updateMany({}, { $set: { avatar: avatar } }).then((result) => {
      res.json({
        code: 200,
        msg: "上传成功",
        type: 1,
        result,
        avatar,
      });
    });
  } else {
    res.json({
      code: 500,
      msg: "上传失败",
      type: 0,
    });
  }
});

//获取所有用户信息
router.get("/getprousers", (req, res) => {
  checkToken(req, res, (data) => {
    Prouser.find({}, {})
      .sort({
        _id: -1,
      })
      .then((result) => {
        res.json({
          code: 200,
          type: 1,
          msg: "查询用户列表成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

//获取所有用户信息+体温
router.get("/getprovolume", (req, res) => {
  checkToken(req, res, (data) => {
    Volume.find({}, {})
      .sort({
        _id: -1,
      })
      .then((result) => {
        res.json({
          code: 200,
          type: 1,
          msg: "查询用户列表成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

// 添加用户
router.post("/adduserone", (req, res) => {
  checkToken(req, res, (data) => {
    var body = req.body;
    body.time = new Date();
    Prouser.findOne({
      phone: body.phone,
    }).then((data) => {
      if (data) {
        res.json({
          code: 200,
          msg: "当前用户已经存在,请勿重复添加",
          type: 0,
          result: data,
        });
      } else {
        Prouser.insertMany(body)
          .then((obj) => {
            Prouser.find({}, {})
              .sort({
                _id: -1,
              })
              .then((result) => {
                res.json({
                  code: 200,
                  type: 1,
                  msg: "新增用户成功",
                  result,
                });
              })
              .catch((err) => {
                res.json({
                  code: 500,
                  msg: "服务器异常",
                  type: 0,
                  err,
                });
              });
          })
          .catch((err) => {
            res.json({
              code: 500,
              msg: "服务器异常",
              type: 0,
              err,
            });
          });
      }
    });
  });
});

// 修改
router.post("/updateuserone", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Prouser.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          ...body,
        },
      }
    )
      .then((result) => {
        res.json({
          code: 200,
          type: 1,
          msg: "修改用户成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

//删除
router.post("/deluserone", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Prouser.remove({
      _id: body._id,
    })
      .then((result) => {
        res.json({
          code: 200,
          type: 1,
          msg: "删除用户成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

// 添加 体温记录
router.post("/volume", (req, res) => {
  var body = req.body;
  body.time = new Date();
  console.log("body:");
  console.log(body);
  Volume.insertMany(body)
    .then((result) => {
      Volume.find({
        phone: body.phone,
      })
        .sort({ _id: -1 })
        .then((result) => {
          res.json({
            code: 200,
            type: 1,
            msg: "上传体温记录成功",
            result,
          });
        });
    })
    .catch((err) => {
      res.json({
        code: 500,
        msg: "服务器异常",
        type: 0,
        err,
      });
    });
});

// //添加用户体温信息
// router.post("/volume", (req, res) => {
//     let body = req.body
//     body.time = new Date();
//     Volume.findOne({
//         phone: body.phone
//     }).then(data => {
//         //插入
//         Volume.insertMany(body).then(date => {
//                 console.log("date:")
//                 console.log(date)
//                 Volume.find({}, {})
//                     .sort({
//                         _id: -1
//                     })
//                     .then(result => {
//                         res.json({
//                             code: 200,
//                             type: 1,
//                             msg: '添加成功',
//                             result
//                         })
//                     })
//                     .catch(err => {
//                         res.json({
//                             code: 500,
//                             msg: '服务器异常',
//                             type: 0,
//                             err,
//                         })
//                     })
//             })
//             .catch(err => {
//                 res.json({
//                     code: 500,
//                     msg: '服务器异常',
//                     type: 0,
//                     err,
//                 })
//             })
//     })
// })
// ==============================
// 添加 体温记录
router.post("/addmytiwen", (req, res) => {
  var body = req.body;
  body.time = new Date();
  checkToken(req, res, (data) => {
    Volume.insertMany(body)
      .then((result) => {
        Volume.find({
          phone: body.phone,
        })
          .sort({ _id: -1 })
          .then((result) => {
            res.json({
              code: 200,
              type: 1,
              msg: "上传体温记录成功",
              result,
            });
          });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

// 添加 体温记录
router.post("/findmytiwen", (req, res) => {
  checkToken(req, res, (data) => {
    Volume.find({
      phone: data.phone,
    })
      .sort({ _id: -1 })
      .then((result) => {
        res.json({
          code: 200,
          type: 1,
          msg: "获取我的体温记录成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

// 修改 是否核酸检测
router.post("/checkmyistest", (req, res) => {
  const body = req.body;
  checkToken(req, res, (data) => {
    Volume.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          isTest: body.isTest,
        },
      }
    )
      .then((result) => {
        res.json({
          code: 200,
          type: 1,
          msg: "修改成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

// 修改 体温值
router.post("/changemytiwen", (req, res) => {
  const body = req.body;
  console.log(body);
  checkToken(req, res, (data) => {
    Volume.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          degree: body.degree,
          time: new Date(),
        },
      }
    )
      .then((obj) => {
        Volume.find({
          phone: data.phone,
        })
          .sort({ _id: -1 })
          .then((result) => {
            res.json({
              code: 200,
              type: 1,
              msg: "修改成功",
              result,
            });
          });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

// 所有 体温所有记录
router.post("/findalltiwen", (req, res) => {
  checkToken(req, res, (data) => {
    Volume.find({})
      .sort({ _id: -1 })
      .then((result) => {
        res.json({
          code: 200,
          type: 1,
          msg: "获取体温记录成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

// 查询出所有的公告 
router.post("/findallgonggao", (req, res) => {
  checkToken(req, res, data => {
    GongGao.find({

    })
      .sort({ _id: -1 })
      .then(result => {
        res.json({
          code: 200,
          type: 1,
          msg: '获取公告列表成功',
          result
        })
      })
      .catch(err => {
        res.json({
          code: 500,
          msg: '服务器异常',
          type: 0,
          err,
        })
      })
  })
})

// 添加公告
router.post("/addgonggao", (req, res) => {
  checkToken(req, res, (data) => {
    const { title, content, username, score } = req.body;
    GongGao.insertMany({
      title,
      content,
      score,
      username,
      time: new Date(),
    })
      .then((result) => {
        res.json({
          code: 200,
          msg: "提交公告成功",
          result,
          type: 1,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          type: 0,
          err,
        });
      });
  });
});

// 删除 公告 
router.post("/delgonggaoone", (req, res) => {
  var body = req.body;
  checkToken(req, res, data => {
    GongGao.remove({
      _id: body._id
    })
      .then(result => {
        res.json({
          code: 200,
          type: 1,
          msg: '删除成功',
          result
        })
      })
      .catch(err => {
        res.json({
          code: 500,
          msg: '服务器异常',
          type: 0,
          err,
        })
      })
  })
})

// 查询 公告 
router.post("/findOnegonggao", (req, res) => {
  var body = req.body;
  checkToken(req, res, data => {
    GongGao.findOne({
      _id: body._id
    })
      .then(result => {
        res.json({
          code: 200,
          type: 1,
          msg: '获取公告成功',
          result
        })
      })
      .catch(err => {
        res.json({
          code: 500,
          msg: '服务器异常',
          type: 0,
          err,
        })
      })
  })
})

// 修改 公告 
router.post("/updateOnegonggao", (req, res) => {
  var body = req.body;
  checkToken(req, res, data => {
    GongGao.updateMany({
      _id: body._id
    }, {
      $set: {
        title: body.title,
        content: body.content,
        score: body.score
      }
    })
      .then(result => {
        res.json({
          code: 200,
          type: 1,
          msg: '修改公告成功',
          result
        })
      })
      .catch(err => {
        res.json({
          code: 500,
          msg: '服务器异常',
          type: 0,
          err,
        })
      })
  })
})


// // =====reactproject======
// router.post("/ReactLogin", (req, res) => {
//   var body = req.body
//   Reactuser.findOne({ username: body.username, userpwd: body.userpwd }).then(result => {
//     if (result) {
//       const token = createToken({ userpwd: body.userpwd, date: new Date() });
//       res.json({
//         code: 200,
//         result,
//         msg: "登录成功",
//         token,
//         type: 1
//       })
//     } else {
//       res.json({
//         code: 500,
//         msg: "登录失败,请检查用户名或密码",
//         type: 0
//       })
//     }
//   })
// })
module.exports = router;
